import { IView } from "../../../mvc/interfaces/IView";
import { AutoFight } from "./AutoFight";
import { FightSkillItem } from "./FightSkillItem";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../resource/ImagesConst";
import { DragEvent } from "../../../../com/mui/events/DragEvent";
import { EventName } from "../../mvc/EventName";
import { BaseItem } from "../common/BaseItem";
import { SkillInfo } from "../../model/SkillInfo";
import { MsgManager } from "../../manager/MsgManager";
import { Language } from "../../../../extend/language/Language";
import { SkillsUtil } from "../../scene/skill/SkillsUtil";
import { Cache } from "../../cache/Cache";
import { ILayer } from "../../../mvc/interfaces/ILayer";
import { AutoFightInfo } from "./AutoFightInfo";
type int = number;
//class SkillPanel
    
    export  class SkillPanel /* flash.display.Sprite */ implements IView
    {
        constructor(arg1: int, arg2: Boolean=false, arg3: Boolean=false)
        {
            
            this._skillItemArr = [];
            this._skillIdArr = [];
            this._autoFightInfo = AutoFight.localAutoFight;
            this._skillNum = arg1;
            this._isStateInc = arg2;
            this._isMultiline = arg3;
            var loc1=0;
            while (loc1 < arg1) 
            {
                this._skillIdArr[loc1] = 0;
                ++loc1;
            }
            this.init();
            return;
        }

        public get skillIdArr(): Array<any>
        {
            return this._skillIdArr;
        }

        /* internal  */init(): void
        {
            var loc3=null;
            var loc1=this.skillX;
            var loc2=this.skillY;
            var loc4=0;
            while (loc4 < this._skillNum) 
            {
                loc3 = new FightSkillItem();
                loc3.bgBitmap = GlobalClass.getBitmapData(ImagesConst.PackItemBg);
                loc3.setSize(32, 32);
                loc3.x = loc1;
                loc3.y = loc2;
                loc3.isDragAble = true;
                loc3.isDropAble = true;
                this.addChild(loc3);
                loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onSkillItemClickHandler);
                this._skillItemArr.push(loc3);
                loc1 = loc1 + 43;
                if (loc4 == 4) 
                {
                    if (this._isMultiline) 
                    {
                        loc1 = this.skillX;
                        loc2 = loc2 + loc3.height + 11;
                    }
                }
                ++loc4;
            }
            this.addEventListener(DragEvent.Event_Move_In, this.onMoveInHandler);
            this.addEventListener(DragEvent.Event_Throw_goods, this.onThrowGoodsHandler);
            return;
        }

        /* internal  */onSkillItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as FightSkillItem;
            if (loc1.dragSource == null) 
            {
                return;
            }
            var loc2=this._skillItemArr.indexOf(loc1);
            if (loc1.isUseable) 
            {
                this._skillIdArr[loc2] = 0;
                this._autoFightInfo.getSkillArr()[this._index][loc2] = 0;
                loc1.isUseable = false;
            }
            else 
            {
                this._skillIdArr[loc2] = this._autoFightInfo.getSkillShowArr()[this._index][loc2];
                this._autoFightInfo.getSkillArr()[this._index][loc2] = this._autoFightInfo.getSkillShowArr()[this._index][loc2];
                loc1.isUseable = true;
            }
            this.dispatchEvent(new Object /* flash.events.Event */(EventName.SkillItemChange));
            return;
        }

        /* internal  */onMoveInHandler(arg1: DragEvent): void
        {
            var loc3=false;
            var loc4=null;
            var loc5=null;
            var loc6=0;
            var loc7=0;
            var loc8=0;
            var loc9=0;
            var loc10=false;
            var loc11=0;
            var loc1=arg1.dragItem as BaseItem;
            if (typeof !(loc1.dragSource === "skillinfo")) 
            {
                return;
            }
            var loc2=arg1.dropItem as FightSkillItem;
            if (this._isStateInc) 
            {
                MsgManager.showRollTipsMsg(Language.getString(31028));
                if (!(loc3 = SkillsUtil.instance.isGain((loc1.dragSource as SkillInfo).skillId))) 
                {
                    return;
                }
            }
            else if (!SkillsUtil.instance.isCanAddToAutoFightList((loc1.dragSource as SkillInfo).skillId)) 
            {
                return;
            }
            if (loc1 && loc2) 
            {
                if (typeof loc1 === "fightskillitem") 
                {
                    loc4 = loc1.dragSource;
                    loc5 = loc2.dragSource;
                    loc6 = this._skillItemArr.indexOf(loc1);
                    loc7 = this._skillItemArr.indexOf(loc2);
                    loc2.dragSource = loc4;
                    loc1.dragSource = loc5;
                    loc8 = this._skillIdArr[loc6];
                    this._skillIdArr[loc6] = this._skillIdArr[loc7];
                    this._skillIdArr[loc7] = loc8;
                    this._autoFightInfo.getSkillArr()[this._index][loc6] = this._skillIdArr[loc6];
                    this._autoFightInfo.getSkillArr()[this._index][loc7] = this._skillIdArr[loc7];
                    loc9 = this._autoFightInfo.getSkillShowArr()[this._index][loc6];
                    this._autoFightInfo.getSkillShowArr()[this._index][loc6] = this._autoFightInfo.getSkillShowArr()[this._index][loc7];
                    this._autoFightInfo.getSkillShowArr()[this._index][loc7] = loc9;
                    loc10 = (loc1 as FightSkillItem).isUseable;
                    (loc1 as FightSkillItem).isUseable = (loc2 as FightSkillItem).isUseable;
                    (loc2 as FightSkillItem).isUseable = loc10;
                }
                else 
                {
                    loc2.dragSource = loc1.dragSource;
                    loc11 = this._skillItemArr.indexOf(loc2);
                    this._skillIdArr[loc11] = (loc1.dragSource as SkillInfo).skillId;
                    this._autoFightInfo.getSkillArr()[this._index][loc11] = this._skillIdArr[loc11];
                    this._autoFightInfo.getSkillShowArr()[this._index][loc11] = (loc1.dragSource as SkillInfo).skillId;
                }
                this.dispatchEvent(new Object /* flash.events.Event */(EventName.SkillItemChange));
            }
            return;
        }

        /* internal  */onThrowGoodsHandler(arg1: DragEvent): void
        {
            var loc1=arg1.dragItem as FightSkillItem;
            var loc2=arg1.dropItem as BaseItem;
            if (loc1 && loc2 == null) 
            {
                this.clearItem(loc1);
                this.dispatchEvent(new Object /* flash.events.Event */(EventName.SkillItemChange));
            }
            return;
        }

        public clearItem(arg1: FightSkillItem): void
        {
            var loc1=this._skillItemArr.indexOf(arg1);
            this._skillIdArr[loc1] = 0;
            this._autoFightInfo.getSkillArr()[this._index][loc1] = 0;
            this._autoFightInfo.getSkillShowArr()[this._index][loc1] = 0;
            arg1.isUseable = true;
            arg1.destroy();
            return;
        }

        public showSkill(arg1: int, arg2: Boolean=true): void
        {
            var loc1=null;
            var loc2=null;
            var loc6=null;
            this._index = arg1;
            if (arg2) 
            {
                loc1 = this._autoFightInfo.getSkillArr()[arg1];
                loc2 = this._autoFightInfo.getSkillShowArr()[arg1];
            }
            else 
            {
                loc1 = AutoFight.currentAutoFight.getSkillArr()[arg1];
                loc2 = AutoFight.currentAutoFight.getSkillShowArr()[arg1];
            }
            if (loc1 == null || loc2 == null) 
            {
                return;
            }
            var loc3=loc1.length;
            this._skillIdArr.splice(0);
            var loc4=0;
            while (loc4 < this._skillNum) 
            {
                this._skillIdArr.push(0);
                (this._skillItemArr[loc4] as FightSkillItem).isUseable = true;
                ++loc4;
            }
            var loc5=0;
            while (loc5 < loc3) 
            {
                if (!(loc2[loc5] == 0) && !(loc2[loc5] == null)) 
                {
                    if ((loc6 = Cache.instance.skill.getSkillInfoById(loc2[loc5])) && !(this._skillItemArr[loc5] == null)) 
                    {
                        (this._skillItemArr[loc5] as FightSkillItem).dragSource = loc6;
                    }
                    if (loc1[loc5] == 0 && !(loc1[loc5] == null)) 
                    {
                        (this._skillItemArr[loc5] as FightSkillItem).isUseable = false;
                    }
                }
                if (!(loc1[loc5] == 0) && !(loc1[loc5] == null)) 
                {
                    this._skillIdArr[loc5] = loc1[loc5];
                }
                ++loc5;
            }
            return;
        }

        public update(arg1: Object, ... rest): void
        {
            return;
        }

        public hide(): void
        {
            return;
        }

        public show(arg1: int=0, arg2: int=0): void
        {
            return;
        }

        public set layer(arg1: ILayer)
        {
            return;
        }

        public get layer(): ILayer
        {
            return null;
        }

        public get isHide(): Boolean
        {
            return false;
        }

        public clean(): void
        {
            this._skillIdArr = [];
            var loc1=0;
            while (loc1 < this._skillItemArr.length) 
            {
                (this._skillItemArr[loc1] as FightSkillItem).dragSource = null;
                ++loc1;
            }
            return;
        }

        public dispose(arg1: Boolean=true): void
        {
            return;
        }

        /* internal */ /* const */skillX: int=3;

        /* internal */ /* const */skillY: int=3;

        /* internal */ /* var */_skillNum: int=0;

        /* internal */ /* var */_skillItemArr: Array<any>;

        /* internal */ /* var */_skillIdArr: Array<any>;

        /* internal */ /* var */_isStateInc: Boolean;

        /* internal */ /* var */_isMultiline: Boolean;

        /* internal */ /* var */_index: int=0;

        /* internal */ /* var */_autoFightInfo: AutoFightInfo;
    }
